package com.dragon.mmochat.service;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.MmochatMain;
import com.dragon.mmochat.message.Mmochat_5_14S;
import com.dragon.mmochat.message.Mmochat_5_30C;
import com.dragon.mmochat.message.Mmochat_9_55C;
import com.dragon.mmochat.message.Mmochat_9_56C;
import com.dragon.mmochat.message.Mmochat_9_57C;
import com.dragon.mmochat.message.Mmochat_9_58C;
import com.dragon.mmochat.message.Mmochat_9_59C;
import com.dragon.mmochat.message.Mmochat_9_60C;
import com.dragon.mmochat.model.MmochatDailyActivity;
import com.dragon.mmochat.model.enumType.MmochatDailyActivityType;
import com.dragon.mmochat.model.enumType.MmochatDrugType;
import com.dragon.mmochat.model.enumType.MmochatLifeSkillType;
import com.dragon.mmochat.model.enumType.MmochatMaterialType;
import com.dragon.mmochat.model.enumType.MmochatObjOwnerState;
import com.dragon.mmochat.model.enumType.MmochatPetType;
import com.dragon.mmochat.model.enumType.MmochatPropType;
import com.dragon.mmochat.model.enumType.MmochatRoleType;
import com.dragon.mmochat.model.object.MmochatCommonObject;
import com.dragon.mmochat.model.object.MmochatProp;
import com.dragon.mmochat.model.role.MmochatPet;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.model.skill.lifeSkill.MmochatLifeSkill;
import com.dragon.mmochat.model.skill.lifeSkill.MmochatLifeSkillData;
import com.dragon.mmochat.util.CreateTlvWin;
import com.dragon.mmochat.util.MmochatUtil;
import com.skymobi.handler.SkymobiHandler;
import com.skymobi.message.SkymobiProtocolMessage;
import com.skymobi.message.widget.AddDataTLV;
import com.skymobi.message.widget.ClearDataTLV;
import com.skymobi.message.widget.CloseWinTLV;
import com.skymobi.message.widget.CreateMsgBoxTLV;
import com.skymobi.message.widget.SendDataTLV;
import com.skymobi.message.widget.ShowWaitingMsgBoxTLV;
import com.skymobi.message.widget.WinType;
import com.skymobi.service.SkymobiService;
import com.skymobi.util.Text;

//生活技能
public class MmochatLifeSkillService {
	private static Logger log = LoggerFactory
			.getLogger(MmochatLifeSkillService.class);

	// 技能数值:Map<技能类型, Map<技能等级, 技能数值>>
	public static Map<MmochatLifeSkillType, Map<Integer, MmochatLifeSkillData>> skillData = new ConcurrentHashMap<MmochatLifeSkillType, Map<Integer, MmochatLifeSkillData>>();

	public MmochatLifeSkillService() {
		if (!MmochatMain.openMergeServer) {
			// 加载技能数值
			log.debug("加载生活技能数值");
			loadSkillDataFromFolder("skill/lifeSkill");
		}
	}

	// 加载文件夹下的所有数值文件
	public static void loadSkillDataFromFolder(String folder) {
		File root;
		File[] files;
		Pattern fileNamePattern = Pattern.compile("[^/]*\\.");
		root = new File(folder);
		files = root.listFiles();
		if (files != null) {
			for (File file : files) {
				if (!file.isFile()) {
					continue;
				}
				String fileName = file.getName();
				Matcher matcher = fileNamePattern.matcher(fileName);
				if (matcher.find()) {
					String fileStr = matcher.group();
					fileStr = fileStr.substring(0, fileStr.length() - 1);
					log.debug("技能名:" + fileStr);
					MmochatLifeSkillType skillType = MmochatLifeSkillType
							.getSkillByShortName(fileStr);
					if (skillType == null) {
						throw new RuntimeException(folder + "," + fileStr
								+ "：没有对应的技能");
					}
					skillData.put(skillType, parseSkillDataFromFile(file,
							skillType));
				}
			}
		}
	}

	// 解析一个技能数值文件
	private static Map<Integer, MmochatLifeSkillData> parseSkillDataFromFile(
			File file, MmochatLifeSkillType skillType) {
		Map<Integer, MmochatLifeSkillData> map = new ConcurrentHashMap<Integer, MmochatLifeSkillData>();
		if (file.exists()) {
			if (file.isFile()) {
				try {
					BufferedReader reader = new BufferedReader(new FileReader(
							file));
					String lineBuf;
					int maxSkillLevel = 0;
					int maxKeepRound = 0;
					int maxDefendTimes = 0;
					double maxEffectValue = 0;
					reader.readLine();
					while ((lineBuf = reader.readLine()) != null) {
						if (lineBuf.trim() == "") {
							continue;
						}
						String[] array = lineBuf.split("\t");
						if (array != null && array.length > 0) {
							if (array.length != 6) {
								throw new RuntimeException(file.getName()
										+ "中的技能数值项，不是6项");
							}
							double[] values = new double[array.length];
							for (int i = 0; i < array.length; i++) {
								try {
									values[i] = Double.parseDouble(array[i]);
								} catch (NumberFormatException e) {
									e.printStackTrace();
									throw new RuntimeException(file.getName()
											+ "中有非数字!");
								}
							}
							MmochatLifeSkillData data = new MmochatLifeSkillData();
							data.setSkillLevel((int) values[0]);// 技能等级
							data.setUplevelExp((int) values[1]);// 升级经验
							data.setKeepRound((int) values[2]);// 持续回合数
							data.setDefendTimes((int) values[3]);// 可抵抗回合数
							data.setCostSprit((int) values[4]);// 消耗法力
							data.setEffectValue(values[5]);// 技能效果值
							map.put(data.getSkillLevel(), data);
							maxSkillLevel = (int) Math.max(maxSkillLevel,
									values[0]);
							maxKeepRound = (int) Math.max(maxKeepRound,
									values[2]);
							maxDefendTimes = (int) Math.max(maxDefendTimes,
									values[3]);
							maxEffectValue = Math
									.max(maxEffectValue, values[5]);
						}
					}
					skillType.setMaxLevel(maxSkillLevel);
					skillType.setMaxDefendTimes(maxDefendTimes);
					skillType.setMaxEffectValue(maxEffectValue);
					skillType.setMaxKeepRound(maxKeepRound);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
					throw new RuntimeException("文件缺失!文件名:" + file.getName());
				} catch (IOException e) {
					e.printStackTrace();
					throw new RuntimeException("读取出错!文件名:" + file.getName());
				}
			} else {
				throw new RuntimeException("文件缺失!文件名:" + file.getName());
			}
		} else {
			throw new RuntimeException("文件缺失!文件名:" + file.getName());
		}
		return map;
	}

	// 获得升级所需经验
	public static int getUplevelExpNeed(MmochatLifeSkillType type, int curLevel) {
		int value = 0;
		if (curLevel == 0) {
			return 0;
		}
		if (curLevel >= type.getMaxLevel()) {
			return 2000000000;
		}
		try {
			MmochatLifeSkillData skillData = MmochatLifeSkillService.skillData
					.get(type).get(curLevel + 1);
			value = skillData.getUplevelExp();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}

	// 获得持续回合数
	public static int getKeepRound(MmochatCommonObject prop) {
		if (prop == null || prop.getPropType() == null
				|| prop.getPropLevel() == null) {
			return 0;
		}
		switch (prop.getPropType()) {
		case 三魂丹: {
			return getKeepRound(MmochatLifeSkillType.三魂炼丹术, prop.getPropLevel());
		}
		case 七魄丹: {
			return getKeepRound(MmochatLifeSkillType.七魄炼丹术, prop.getPropLevel());
		}
		case 妙手回春丹: {
			return getKeepRound(MmochatLifeSkillType.妙手回春炼丹术, prop
					.getPropLevel());
		}
		case 魂魄归位丹: {
			return getKeepRound(MmochatLifeSkillType.魂魄归位炼丹术, prop
					.getPropLevel());
		}
		default:
			return 0;
		}
	}

	public static int getKeepRound(MmochatLifeSkillType type, int level) {
		int value = 0;
		if (level == 0) {
			return 0;
		}
		try {
			MmochatLifeSkillData skillData = MmochatLifeSkillService.skillData
					.get(type).get(level);
			value = skillData.getKeepRound();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}

	// 获得可抵抗次数
	public static int getDefendTimes(MmochatCommonObject prop) {
		if (prop == null || prop.getPropType() == null
				|| prop.getPropLevel() == null) {
			return 0;
		}
		switch (prop.getPropType()) {
		case 三魂丹: {
			return getDefendTimes(MmochatLifeSkillType.三魂炼丹术, prop
					.getPropLevel());
		}
		case 七魄丹: {
			return getDefendTimes(MmochatLifeSkillType.七魄炼丹术, prop
					.getPropLevel());
		}
		case 妙手回春丹: {
			return getDefendTimes(MmochatLifeSkillType.妙手回春炼丹术, prop
					.getPropLevel());
		}
		case 魂魄归位丹: {
			return getDefendTimes(MmochatLifeSkillType.魂魄归位炼丹术, prop
					.getPropLevel());
		}
		default:
			return 0;
		}
	}

	public static int getDefendTimes(MmochatLifeSkillType type, int level) {
		int value = 0;
		if (level == 0) {
			return 0;
		}
		try {
			MmochatLifeSkillData skillData = MmochatLifeSkillService.skillData
					.get(type).get(level);
			value = skillData.getDefendTimes();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}

	// 获得消耗法力
	public static int getCostSprit(MmochatCommonObject prop) {
		if (prop == null || prop.getPropType() == null
				|| prop.getPropLevel() == null) {
			return 0;
		}
		switch (prop.getPropType()) {
		case 三魂丹: {
			return getCostSprit(MmochatLifeSkillType.三魂炼丹术, prop.getPropLevel());
		}
		case 七魄丹: {
			return getCostSprit(MmochatLifeSkillType.七魄炼丹术, prop.getPropLevel());
		}
		case 妙手回春丹: {
			return getCostSprit(MmochatLifeSkillType.妙手回春炼丹术, prop
					.getPropLevel());
		}
		case 魂魄归位丹: {
			return getCostSprit(MmochatLifeSkillType.魂魄归位炼丹术, prop
					.getPropLevel());
		}
		default:
			return 0;
		}
	}

	public static int getCostSprit(MmochatLifeSkillType type, int level) {
		int value = 0;
		if (level == 0) {
			return 0;
		}
		try {
			MmochatLifeSkillData skillData = MmochatLifeSkillService.skillData
					.get(type).get(level);
			value = skillData.getCostSprit();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return value;
	}

	// 获得技能效果值
	public static double getEffectValue(MmochatCommonObject prop) {
		if (prop == null || prop.getPropType() == null
				|| prop.getPropLevel() == null) {
			return 0;
		}
		switch (prop.getPropType()) {
		case 三魂丹: {
			return getEffectValue(MmochatLifeSkillType.三魂炼丹术, prop
					.getPropLevel());
		}
		case 七魄丹: {
			return getEffectValue(MmochatLifeSkillType.七魄炼丹术, prop
					.getPropLevel());
		}
		case 妙手回春丹: {
			return getEffectValue(MmochatLifeSkillType.妙手回春炼丹术, prop
					.getPropLevel());
		}
		case 魂魄归位丹: {
			return getEffectValue(MmochatLifeSkillType.魂魄归位炼丹术, prop
					.getPropLevel());
		}
		default:
			return 0;
		}
	}

	public static double getEffectValue(MmochatLifeSkillType type, int level) {
		double value = 0;
		if (level == 0) {
			return 0;
		}
		try {
			MmochatLifeSkillData skillData = MmochatLifeSkillService.skillData
					.get(type).get(level);
			value = skillData.getEffectValue();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}

	// 查看技能
	@SkymobiService
	public SkymobiProtocolMessage handleC_9_55(SkymobiHandler handler,
			Mmochat_9_55C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		MmochatLifeSkillType skillType = MmochatLifeSkillType.get(req
				.getSkillType());
		if (skillType == null) {
			return MmochatUtil.msgbox(req.getHallState(), "无此技能!");
		}
		String title = skillType.toString();
		CreateTlvWin tlv = new CreateTlvWin();
		tlv.setWinType(WinType.List);
		tlv.setLeftName("查看");
		tlv.setRightName("返回");
		tlv.setTitle(title);
		tlv.setWinHeight(188);
		tlv.addRightTLV(new CloseWinTLV());

		tlv.addItemEvent("炼丹术介绍", new ClearDataTLV(), new AddDataTLV((int) 4));
		tlv.addItemEvent("我的炼丹术", new ClearDataTLV(), new AddDataTLV((int) 1));
		tlv.addItemEvent("每日修炼", new ClearDataTLV(), new AddDataTLV((int) 2));
		tlv.addItemEvent("我要炼丹", new ClearDataTLV(), new AddDataTLV((int) 3));
		tlv.addLeftTLV(new AddDataTLV((int) req.getSkillType()));
		tlv.addLeftTLV(new SendDataTLV(MmochatConstant.getClientModuleId(),
				MmochatMain.getModuleInstID(),
				MmochatConstant.MSGCODE_MMOCHAT_C_9_56, (int) me.getRoleId()));
		ShowWaitingMsgBoxTLV wait = new ShowWaitingMsgBoxTLV();
		tlv.addLeftTLV(wait);
		return MmochatUtil.tlvResponse(req.getHallState(), tlv);
	}

	// 查看我的炼丹术信息
	public CreateTlvWin getLifeSkillTlv(MmochatPlayer me,
			MmochatLifeSkillType skillType) {
		MmochatLifeSkill skill = me.getLifeSkill(skillType);
		if (skill == null) {
			return null;
		}
		int level = skill.getLevel();
		// 我的炼丹术
		CreateTlvWin tlv = new CreateTlvWin();
		tlv.setWinType(WinType.Text);
		tlv.setLeftName("升级");
		tlv.setRightName("返回");
		tlv.setTitle("我的炼丹术");
		tlv.setWinHeight(188);
		tlv.setWinId(MmochatConstant.TLV_WIN_LIFE_SKILL1);
		tlv.addRightTLV(new CloseWinTLV(MmochatConstant.TLV_WIN_LIFE_SKILL1));
		String content = MmochatUtil.wrapColor(skillType.toString() + "\n",
				Color.yellow);
		content += MmochatUtil.wrapColor("术法等级:", Color.magenta);
		content += skill.getLevel() + "级\n";
		content += MmochatUtil.wrapColor("升级经验:", Color.magenta);
		if (skill.getLevel() == skillType.getMaxLevel()) {
			content += "-";
		} else {
			int levelUpExpNeed = MmochatLifeSkillService.getUplevelExpNeed(
					skillType, skill.getLevel());
			content += skill.getExp() + "/" + levelUpExpNeed;
		}
		content += "\n";
		content += MmochatUtil.wrapColor("炼丹时间:", Color.magenta);
		content += (MmochatConstant.lifeDrugCreateTime / MmochatConstant.MS_PER_HOUR)
				+ "小时\n";
		content += MmochatUtil.wrapColor("丹药时效:", Color.magenta);
		content += (MmochatConstant.lifeDrugEffectTime / MmochatConstant.MS_PER_HOUR)
				+ "小时内，可不限次数反复使用\n";
		content += MmochatUtil.wrapColor("丹药效果:", Color.magenta);
		int keepRound = MmochatLifeSkillService.getKeepRound(skillType, level);
		int defendRound = MmochatLifeSkillService.getDefendTimes(skillType,
				level);
		double effectValue = MmochatLifeSkillService.getEffectValue(skillType,
				level);
		int costSprit = MmochatLifeSkillService.getCostSprit(skillType, level);
		content += "战斗中对本队成员使用，";
		switch (skillType) {
		case 三魂炼丹术: {
			content += "角色真身变为猛虎替身，由替身承受物攻伤害，角色真身不受任何物攻伤害。此状态最多保持" + keepRound
					+ "回合，最多承受" + defendRound + "次物理攻击，累积最多承受"
					+ (int) effectValue + "物理伤害。回合数、"
					+ "攻击次数或累积伤害任一条件达到上限，此状态消失。技能等级越高，"
					+ "则回合数、抵御次数、累积伤害也越高。\n";
			break;
		}
		case 七魄炼丹术: {
			content += "角色真身变为仙草替身，由替身承受法攻伤害，角色真身不受任何法攻伤害。此状态最多保持" + keepRound
					+ "回合，最多承受" + defendRound + "次法术攻击，累积最多承受"
					+ (int) effectValue + "法术伤害。回合数、"
					+ "攻击次数或累积伤害任一条件达到上限，此状态消失。技能等级越高，"
					+ "则回合数、抵御次数、累积伤害也越高。\n";
			break;
		}
		case 妙手回春炼丹术: {
			content += "每回合自动回复一定比例气血。此状态可保持" + keepRound + "回合，首回合可恢复"
					+ (int) (effectValue * 100) + "%的气血，后续每回合恢复气血为上一回合的一半。"
					+ "如果角色已死亡，则回复气血同时救活此角色，并且此状态立即消失。" + "技能等级越高，"
					+ "则回合数、回血比例也越高。\n";
			break;
		}
		case 魂魄归位炼丹术: {
			content += "在使用后的任一回合，使用障碍技能成功障碍对方时，如果对方身上有三魂丹、"
					+ "七魄丹或妙手回春术效果，且你的魂魄归位丹等级不低于对方丹药等级，"
					+ "则可使对方丹药效果立即失效，并且你的魂魄归位丹效果也同时消失。\n";
			break;
		}
		}
		content += MmochatUtil.wrapColor("法力消耗:", Color.magenta);
		content += "使用此丹药需消耗使用者" + costSprit + "点法力!\n\n";

		if (level < skillType.getMaxLevel()) {
			level++;
			content += MmochatUtil.wrapColor("下级炼丹术\n", Color.yellow);
			content += MmochatUtil.wrapColor("丹药效果:\n", Color.magenta);
			keepRound = MmochatLifeSkillService.getKeepRound(skillType, level);
			defendRound = MmochatLifeSkillService.getDefendTimes(skillType,
					level);
			effectValue = MmochatLifeSkillService.getEffectValue(skillType,
					level);
			costSprit = MmochatLifeSkillService.getCostSprit(skillType, level);
			switch (skillType) {
			case 三魂炼丹术: {
				content += "保持回合数:" + keepRound + "/"
						+ skillType.getMaxKeepRound() + "\n";
				content += "抵御物攻次数:" + defendRound + "/"
						+ skillType.getMaxDefendTimes() + "\n";
				content += "累积抵御伤害:\n" + (int) effectValue + "/"
						+ (int) skillType.getMaxEffectValue() + "\n";
				break;
			}
			case 七魄炼丹术: {
				content += "保持回合数:" + keepRound + "/"
						+ skillType.getMaxKeepRound() + "\n";
				content += "抵御法攻次数:" + defendRound + "/"
						+ skillType.getMaxDefendTimes() + "\n";
				content += "累积抵御伤害:\n" + (int) effectValue + "/"
						+ (int) skillType.getMaxEffectValue() + "\n";
				break;
			}
			case 妙手回春炼丹术: {
				content += "保持回合数:" + keepRound + "/"
						+ skillType.getMaxKeepRound() + "\n";
				content += "首次回血比例:\n" + (int) (effectValue * 100) + "%" + "/"
						+ (int) (skillType.getMaxEffectValue() * 100) + "%\n";
				break;
			}
			case 魂魄归位炼丹术: {
				content += "在使用后的任一回合，使用障碍技能成功障碍对方时，如果对方身上有三魂丹、"
						+ "七魄丹或妙手回春丹效果，且你的魂魄归位丹等级不低于对方丹药等级，"
						+ "则可使对方丹药效果立即失效，并且你的魂魄归位丹效果也同时消失。\n";
				break;
			}
			}
		}
		content += MmochatUtil.wrapColor("法力消耗:", Color.magenta);
		content += costSprit + "点";
		tlv.setContent(new Text(content));

		tlv.addLeftTLV(new ClearDataTLV());
		tlv.addLeftTLV(new AddDataTLV((int) skillType.getValue()));
		tlv.addLeftTLV(new SendDataTLV(MmochatConstant.getClientModuleId(),
				MmochatMain.getModuleInstID(),
				MmochatConstant.MSGCODE_MMOCHAT_C_9_57, (int) me.getRoleId()));
		ShowWaitingMsgBoxTLV wait = new ShowWaitingMsgBoxTLV();
		wait.setShowCancel(false);
		tlv.addLeftTLV(wait);

		return tlv;

	}

	// 我要炼丹
	public CreateTlvWin getMakeDrugTlv(MmochatPlayer me,
			MmochatLifeSkillType skillType) {
		MmochatLifeSkill skill = me.getLifeSkill(skillType);
		if (skill == null) {
			return null;
		}
		CreateTlvWin tlv = new CreateTlvWin();
		tlv.setWinType(WinType.Text);
		tlv.setRightName("返回");
		tlv.setTitle("我要炼丹");
		tlv.setWinId(MmochatConstant.TLV_WIN_LIFE_SKILL2);
		tlv.setWinHeight(188);
		tlv.addRightTLV(new CloseWinTLV(MmochatConstant.TLV_WIN_LIFE_SKILL2));
		String content = "";
		MmochatDailyActivityType actType = null;
		List<MmochatMaterialType> materialsNeed = new ArrayList<MmochatMaterialType>();
		List<MmochatDrugType> drugsNeed = new ArrayList<MmochatDrugType>();
		String petNeedInfo = "";
		MmochatPropType propType = null;
		switch (skillType) {
		case 三魂炼丹术: {
			actType = MmochatDailyActivityType.炼制三魂丹;
			materialsNeed.add(MmochatMaterialType.灵兽内丹);
			drugsNeed.add(MmochatDrugType.补气丹);
			propType = MmochatPropType.三魂丹;
			petNeedInfo = "野兽(药兽)，产自北郊。将药兽设置为参战\n";
			break;
		}
		case 七魄炼丹术: {
			actType = MmochatDailyActivityType.炼制七魄丹;
			materialsNeed.add(MmochatMaterialType.巨兽皮);
			drugsNeed.add(MmochatDrugType.补血丸);
			propType = MmochatPropType.七魄丹;
			petNeedInfo = "食人草(药兽)，产自西郊。将药兽设置为参战\n";
			break;
		}
		case 妙手回春炼丹术: {
			actType = MmochatDailyActivityType.炼制回春丹;
			materialsNeed.add(MmochatMaterialType.女妖之心);
			drugsNeed.add(MmochatDrugType.人参果);
			propType = MmochatPropType.妙手回春丹;
			petNeedInfo = "树精(药兽)或树怪(药兽)，产自东郊。将药兽设置为参战\n";
			break;
		}
		case 魂魄归位炼丹术: {
			actType = MmochatDailyActivityType.炼制归位丹;
			materialsNeed.add(MmochatMaterialType.疯神玉简);
			drugsNeed.add(MmochatDrugType.菩提果);
			propType = MmochatPropType.魂魄归位丹;
			petNeedInfo = "树精(药兽)或树怪(药兽)，产自东郊。将药兽设置为参战\n";
			break;
		}
		default:
			break;
		}
		int level = skill.getLevel();
		int keepRound = MmochatLifeSkillService.getKeepRound(skillType, level);
		int defendRound = MmochatLifeSkillService.getDefendTimes(skillType,
				level);
		double effectValue = MmochatLifeSkillService.getEffectValue(skillType,
				level);
		int costSprit = MmochatLifeSkillService.getCostSprit(skillType, level);

		MmochatDailyActivity activity = me.getDailyActivities().get(actType);
		if (activity == null) {
			// 第一次参加此活动
			activity = new MmochatDailyActivity();
			activity.setType(actType);
			me.addDailyActivity(activity);
		}

		if (!activity.isInActivity()) {
			// 没在炼丹
			content += MmochatUtil.wrapColor("当前可以炼丹!\n\n", Color.magenta);
			content += MmochatUtil.wrapColor("[需要材料]\n", Color.yellow);
			for (MmochatMaterialType mType : materialsNeed) {
				content += "20个" + mType.toString() + "\n";
			}
			content += MmochatUtil.wrapColor("[需要药品]\n", Color.yellow);
			for (MmochatDrugType dType : drugsNeed) {
				content += "20个" + dType.toString() + "(药店有售)\n";
			}
			content += MmochatUtil.wrapColor("[需要药兽]\n", Color.yellow);
			content += petNeedInfo;
			content += MmochatUtil.wrapColor("[炼丹时间]\n", Color.yellow);
			content += (MmochatConstant.lifeDrugCreateTime / MmochatConstant.MS_PER_HOUR)
					+ "小时(允许离线)\n\n";
			tlv.setLeftName("炼丹");
			tlv.addLeftTLV(new ClearDataTLV());
			tlv.addLeftTLV(new AddDataTLV((int) skillType.getValue()));
			tlv.addLeftTLV(new SendDataTLV(MmochatConstant.getClientModuleId(),
					MmochatMain.getModuleInstID(),
					MmochatConstant.MSGCODE_MMOCHAT_C_9_59, (int) me
							.getRoleId()));
			ShowWaitingMsgBoxTLV wait = new ShowWaitingMsgBoxTLV();
			wait.setShowCancel(false);
			tlv.addLeftTLV(wait);
		} else {
			// 正在炼丹
			long costTime = System.currentTimeMillis() - activity.getLastTime();
			if (costTime < MmochatConstant.lifeDrugCreateTime) {
				// 正在炼丹中
				String time = MmochatUtil
						.getTimeString(MmochatConstant.lifeDrugCreateTime
								- costTime);
				content += MmochatUtil.wrapColor("炼丹中...还需" + time + "\n\n",
						Color.magenta);
			} else {
				// 已经炼成
				content += MmochatUtil
						.wrapColor("丹药炼成!请领取!\n\n", Color.magenta);
			}
			tlv.setLeftName("领取");
			tlv.addLeftTLV(new ClearDataTLV());
			tlv.addLeftTLV(new AddDataTLV((int) skillType.getValue()));
			tlv.addLeftTLV(new SendDataTLV(MmochatConstant.getClientModuleId(),
					MmochatMain.getModuleInstID(),
					MmochatConstant.MSGCODE_MMOCHAT_C_9_60, (int) me
							.getRoleId()));
			ShowWaitingMsgBoxTLV wait = new ShowWaitingMsgBoxTLV();
			wait.setShowCancel(false);
			tlv.addLeftTLV(wait);
		}

		content += MmochatUtil.wrapColor("[生成丹药]\n", Color.green);
		content += skill.getLevel() + "级" + propType.toString()
				+ "(以领丹时炼丹术等级为准)\n";
		content += MmochatUtil.wrapColor("[丹药效果]\n", Color.green);
		switch (skillType) {
		case 三魂炼丹术: {
			content += "持续回合数:" + keepRound + "\n";
			content += "抵御物攻次数:" + defendRound + "\n";
			content += "累积抵御物伤:\n" + (int) effectValue + "\n";
			content += "消耗法力:" + costSprit + "\n";
			content += "(效果以领丹时炼丹术等级为准)";
			break;
		}
		case 七魄炼丹术: {
			content += "持续回合数:" + keepRound + "\n";
			content += "抵御法攻次数:" + defendRound + "\n";
			content += "累积抵御法伤:\n" + (int) effectValue + "\n";
			content += "消耗法力:" + costSprit + "\n";
			content += "(效果以领丹时炼丹术等级为准)";
			break;
		}
		case 妙手回春炼丹术: {
			content += "持续回合数:" + keepRound + "\n";
			content += "首次回血比例:\n" + (int) (100 * effectValue) + "%\n";
			content += "消耗法力:" + costSprit + "\n";
			content += "(效果以领丹时炼丹术等级为准)";
			break;
		}
		case 魂魄归位炼丹术: {
			content += "成功障碍对方时，可使对方身上丹药等级" + level + "级或以下的所有丹药效果消失\n";
			content += "(效果以领丹时炼丹术等级为准)";
			break;
		}
		default:
			break;
		}
		content += "\n";
		content += MmochatUtil.wrapColor("[丹药时效]\n", Color.green);
		content += (MmochatConstant.lifeDrugEffectTime / MmochatConstant.MS_PER_HOUR)
				+ "小时内无限次使用(首次使用后开始计时)\n";
		tlv.setContent(new Text(content));
		return tlv;
	}

	// 查看我的炼丹术
	@SkymobiService
	public SkymobiProtocolMessage handleC_9_56(SkymobiHandler handler,
			Mmochat_9_56C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		MmochatLifeSkillType skillType = MmochatLifeSkillType.get(req
				.getSkillType());
		if (skillType == null) {
			return MmochatUtil.msgbox(req.getHallState(), "无此炼丹术!");
		}
		MmochatLifeSkill skill = me.getLifeSkill(skillType);
		switch (req.getIndex()) {
		case 1: {
			// 我的炼丹术
			if (skill == null) {
				return MmochatUtil.msgbox(req.getHallState(), "您还没有学得此炼丹术!您可以"
						+ MmochatUtil.wrapColor("按9键进入商城-装备法宝", Color.yellow)
						+ ",购买" + skillType.toString() + "，来获得此炼丹术!");
			}
			CreateTlvWin tlv = getLifeSkillTlv(me, skillType);
			if (tlv != null) {
				return MmochatUtil.tlvResponse(req.getHallState(), tlv);
			}
		}
		case 2: {
			// 每日修练
			if (skill == null) {
				return MmochatUtil.msgbox(req.getHallState(), "您还没有学得此炼丹术!您可以"
						+ MmochatUtil.wrapColor("按9键进入商城-装备法宝", Color.yellow)
						+ ",购买" + skillType.toString() + "，来获得此炼丹术!");
			}
			CreateTlvWin tlv = new CreateTlvWin();
			tlv.setWinType(WinType.Text);
			tlv.setLeftName("修炼");
			tlv.setRightName("返回");
			tlv.setTitle("每日修炼");
			tlv.setWinHeight(188);
			tlv.addRightTLV(new CloseWinTLV());
			String content = "";
			content += MmochatUtil.wrapColor("[每日修炼次数]\n", Color.yellow);
			content += "各类炼丹术每日累积可修炼"
					+ MmochatDailyActivityType.炼丹术.getMaxDailyTimes() + "次\n\n";
			content += MmochatUtil.wrapColor("[修炼方法]\n", Color.yellow);
			switch (skillType) {
			case 三魂炼丹术: {
				content += "去<北郊>打怪时有机率遇到名为<药兽>的怪物，捕捉后设置成参战，再来找我点<修炼>按钮，"
						+ "即可完成修炼。";
				break;
			}
			case 七魄炼丹术: {
				content += "去<西郊>打怪时有机率遇到名为<药兽>的怪物，捕捉后设置成参战，再来找我点<修炼>按钮，"
						+ "即可完成修炼。";
				break;
			}
			case 妙手回春炼丹术: {
				content += "去<东郊>打怪时有机率遇到名为<药兽>的怪物，捕捉后设置成参战，再来找我点<修炼>按钮，"
						+ "即可完成修炼。";
				break;
			}
			case 魂魄归位炼丹术: {
				content += "去<东郊>打怪时有机率遇到名为<药兽>的怪物，捕捉后设置成参战，再来找我点<修炼>按钮，"
						+ "即可完成修炼。";
				break;
			}
			default:
				break;
			}
			content += "\n\n";
			content += MmochatUtil.wrapColor("[修炼奖励]\n", Color.yellow);
			content += "可随机获得30-50点经验，有机率得到100点经验";

			tlv.setContent(new Text(content));
			tlv.addLeftTLV(new ClearDataTLV());
			tlv.addLeftTLV(new AddDataTLV((int) skillType.getValue()));
			tlv.addLeftTLV(new SendDataTLV(MmochatConstant.getClientModuleId(),
					MmochatMain.getModuleInstID(),
					MmochatConstant.MSGCODE_MMOCHAT_C_9_58, (int) me
							.getRoleId()));
			ShowWaitingMsgBoxTLV wait = new ShowWaitingMsgBoxTLV();
			wait.setShowCancel(false);
			tlv.addLeftTLV(wait);
			return MmochatUtil.tlvResponse(req.getHallState(), tlv);
		}
		case 3: {
			// 我要炼丹
			if (skill == null) {
				return MmochatUtil.msgbox(req.getHallState(),
						"您还没有学得此炼丹术!无法进行炼丹!");
			}
			CreateTlvWin tlv = getMakeDrugTlv(me, skillType);
			if (tlv != null) {
				return MmochatUtil.tlvResponse(req.getHallState(), tlv);
			}
		}
		case 4: {
			// 炼丹术介绍
			CreateTlvWin tlv = new CreateTlvWin();
			tlv.setWinType(WinType.Text);
			tlv.setRightName("返回");
			tlv.setTitle(skillType.toString());
			tlv.setWinHeight(188);
			tlv.addRightTLV(new CloseWinTLV());
			String content = "";

			switch (skillType) {
			case 三魂炼丹术: {
				content += MmochatUtil.wrapColor("概述\n", Color.magenta);
				content += "1、术法功能\n";
				content += "2、三魂丹效果\n";
				content += "3、各级三魂丹\n";
				content += "4、特别说明\n\n";
				content += MmochatUtil.wrapColor("详细介绍\n", Color.magenta);
				content += MmochatUtil.wrapColor("[术法功能]\n", Color.yellow);
				content += "可炼制同等级三魂丹\n";
				content += MmochatUtil.wrapColor("[三魂丹效果]\n", Color.yellow);
				content += "使用后角色变身为猛虎，不受物攻伤害。丹药等级越高，"
						+ "此状态持续的回合数越多、可承受的物攻次数越多、累积可承受的物伤也越高。\n";

				content += MmochatUtil.wrapColor("[各级三魂丹]\n", Color.yellow);
				int[] levels = new int[] { 1, 20, 40, 60, 80, 100, 120, 140,
						160, 180 };
				for (int level : levels) {
					content += MmochatUtil.wrapColor(level + "级丹药\n",
							Color.magenta);
					content += MmochatUtil.wrapColor("持续回合数:", Color.green)
							+ (int) getKeepRound(skillType, level) + "\n";
					content += MmochatUtil.wrapColor("抵御物攻次数:", Color.green)
							+ (int) getDefendTimes(skillType, level) + "\n";
					content += MmochatUtil.wrapColor("累积抵御物伤:\n", Color.green)
							+ (int) getEffectValue(skillType, level) + "\n\n";
				}
				content += MmochatUtil.wrapColor("[特别说明]\n", Color.yellow);
				content += "战斗中如果使用了三魂丹，则本场战斗中将无法使用辅助技能。"
						+ "\n另外使用三魂丹时，如果角色处于被障碍状态，则不进行变身，"
						+ "但三魂丹效果仍然有效。直到障碍结束时，再进行变身。"
						+ "\n当三魂丹效果消失时，如果角色处理被障碍状态，则角色不会变回真身，"
						+ "但此时三魂丹效果已消失。直到障碍结束时，再变回真身。";
				break;
			}
			case 七魄炼丹术: {
				content += MmochatUtil.wrapColor("概述\n", Color.magenta);
				content += "1、术法功能\n";
				content += "2、七魄丹效果\n";
				content += "3、各级七魄丹\n";
				content += "4、特别说明\n\n";
				content += MmochatUtil.wrapColor("详细介绍\n", Color.magenta);
				content += MmochatUtil.wrapColor("[术法功能]\n", Color.yellow);
				content += "可炼制同等级七魄丹。\n";
				content += MmochatUtil.wrapColor("[七魄丹效果]\n", Color.yellow);
				content += "使用后角色变身为仙草，不受法攻伤害。丹药等级越高，"
						+ "此状态持续的回合数越多、可承受的法攻次数越多、累积可承受的法伤也越高。\n";
				content += MmochatUtil.wrapColor("[各级七魄丹]\n", Color.yellow);
				int[] levels = new int[] { 1, 20, 40, 60, 80, 100, 120, 140,
						160, 180 };
				for (int level : levels) {
					content += MmochatUtil.wrapColor(level + "级丹药\n",
							Color.magenta);
					content += MmochatUtil.wrapColor("持续回合数:", Color.green)
							+ (int) getKeepRound(skillType, level) + "\n";
					content += MmochatUtil.wrapColor("抵御法攻次数:", Color.green)
							+ (int) getDefendTimes(skillType, level) + "\n";
					content += MmochatUtil.wrapColor("累积抵御法伤:\n", Color.green)
							+ (int) getEffectValue(skillType, level) + "\n\n";
				}
				content += MmochatUtil.wrapColor("[特别说明]\n", Color.yellow);
				content += "战斗中如果使用了七魄丹，则本场战斗中将无法使用辅助技能。"
						+ "\n另外使用七魄丹时，如果角色处于被障碍状态，则不进行变身，"
						+ "但七魄丹效果仍然有效。直到障碍结束时，再进行变身。"
						+ "\n当七魄丹效果消失时，如果角色处理被障碍状态，则角色不会变回真身，"
						+ "但此时七魄丹效果已消失。直到障碍结束时，再变回真身。";
				break;
			}
			case 妙手回春炼丹术: {
				content += MmochatUtil.wrapColor("概述\n", Color.magenta);
				content += "1、术法功能\n";
				content += "2、丹药效果\n";
				content += "3、各级回春丹\n";
				content += "4、如何升级术法\n";
				content += "5、如何炼丹\n";
				content += "6、丹药使用\n\n";
				content += MmochatUtil.wrapColor("[术法功能]\n", Color.yellow);
				content += "可炼制同等级妙手回春丹\n";
				content += MmochatUtil.wrapColor("[丹药效果]\n", Color.yellow);
				content += "使用后每回合末自动回复一定比例气血或复活角色。"
						+ "丹药等级越高，可回血的回合数越多、回血比例越高。\n";
				content += MmochatUtil.wrapColor("[各级回春丹]\n", Color.yellow);
				int[] levels = new int[] { 1, 20, 40, 60, 80, 100, 120, 140,
						160, 180 };
				for (int level : levels) {
					content += MmochatUtil.wrapColor(level + "级丹药\n",
							Color.magenta);
					content += MmochatUtil.wrapColor("持续回合数:", Color.green)
							+ (int) getKeepRound(skillType, level) + "\n";
					content += MmochatUtil.wrapColor("首次回血比例:\n", Color.green)
							+ (int) (100 * getEffectValue(skillType, level))
							+ "%\n\n";
				}
				content += MmochatUtil.wrapColor("[如何升级术法]\n", Color.yellow);
				content += "<每日修炼>可增加一定的术法经验。\n商城<限时出售>中使用相应的炼丹心经也可快速提升经验。\n";

				content += MmochatUtil.wrapColor("[如何炼丹]\n", Color.yellow);
				content += "点击<我要炼丹>即可进行炼丹，炼药期间允许离线。\n";

				content += MmochatUtil.wrapColor("[丹药使用]\n", Color.yellow);
				content += "每个丹药都是限时道具，第一次使用后开始计时，时限内可反复使用。";
				break;
			}
			case 魂魄归位炼丹术: {
				content += MmochatUtil.wrapColor("概述\n", Color.magenta);
				content += "1、术法功能\n";
				content += "2、丹药效果\n";
				content += "3、如何升级术法\n";
				content += "4、如何炼丹\n";
				content += "5、丹药使用\n\n";
				content += MmochatUtil.wrapColor("[术法功能]\n", Color.yellow);
				content += "可炼制同等级魂魄归位丹。\n";
				content += MmochatUtil.wrapColor("[丹药效果]\n", Color.yellow);
				content += "战斗中对本队成员使用，";
				content += "在使用后的任一回合，使用障碍技能成功障碍对方时，如果对方身上有三魂丹、"
						+ "七魄丹或妙手回春丹效果，且你的魂魄归位丹等级不低于对方丹药等级，"
						+ "则可使对方丹药效果立即失效，并且你的魂魄归位丹效果也同时消失。\n";
				content += MmochatUtil.wrapColor("[如何升级术法]\n", Color.yellow);
				content += "<每日修炼>可增加一定的术法经验。\n商城<限时出售>中使用相应的炼丹心经也可快速提升经验。\n";

				content += MmochatUtil.wrapColor("[如何炼丹]\n", Color.yellow);
				content += "点击<我要炼丹>即可进行炼丹，炼药期间允许离线。\n";

				content += MmochatUtil.wrapColor("[丹药使用]\n", Color.yellow);
				content += "每个丹药都是限时道具，第一次使用后开始计时，时限内可反复使用。";
				break;
			}
			}

			tlv.setContent(new Text(content));
			return MmochatUtil.tlvResponse(req.getHallState(), tlv);
		}
		default:
			break;
		}
		return null;
	}

	// 升级技能
	@SkymobiService
	public SkymobiProtocolMessage handleC_9_57(SkymobiHandler handler,
			Mmochat_9_57C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		MmochatLifeSkillType skillType = MmochatLifeSkillType.get(req
				.getSkillType());
		if (skillType == null) {
			return MmochatUtil.msgbox(req.getHallState(), "无此炼丹术!");
		}
		MmochatLifeSkill skill = me.getLifeSkill(skillType);
		if (skill == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有学过此炼丹术!");
		}
		int level = skill.getLevel();
		if (level >= skillType.getMaxLevel()) {
			return MmochatUtil.msgbox(req.getHallState(), "此炼丹术已学满!");
		}
		int expNeed = getUplevelExpNeed(skillType, level);
		if (skill.getExp() < expNeed) {
			return MmochatUtil.msgbox(req.getHallState(), "炼丹术经验未达到升级要求!您可以进行"
					+ "<每日修练>来增加炼丹术经验，或者使用商城<限时出售>的相关道具快速提升经验。");
		}
		// 升级
		skill.addExp(-1 * expNeed);
		skill.addLevel(1);
		try {
			MmochatDao.updateLifeSkill(me);
		} catch (Exception e) {
			e.printStackTrace();
			skill.addLevel(-1);
			skill.addExp(expNeed);
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}

		CreateTlvWin tlv = getLifeSkillTlv(me, skillType);
		if (tlv != null) {
			int costSprit = MmochatLifeSkillService.getCostSprit(skillType,
					skill.getLevel());
			CreateMsgBoxTLV msgbox = new CreateMsgBoxTLV();
			msgbox.setContent(new Text("升级成功!您的"
					+ skillType.toString()
					+ "升到"
					+ MmochatUtil.wrapColor(skill.getLevel() + "级",
							Color.yellow) + "了!当前等级练制的丹药，使用时需要消耗"
					+ MmochatUtil.wrapColor(costSprit + "点法力", Color.green)
					+ "!"));
			msgbox.setLeftName("确定");

			return MmochatUtil.tlvResponse(req.getHallState(), new CloseWinTLV(
					MmochatConstant.TLV_WIN_LIFE_SKILL1), tlv, msgbox);
		}
		return null;
	}

	// 修炼我的炼丹术
	@SuppressWarnings("deprecation")
	@SkymobiService
	public SkymobiProtocolMessage handleC_9_58(SkymobiHandler handler,
			Mmochat_9_58C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		MmochatLifeSkillType skillType = MmochatLifeSkillType.get(req
				.getSkillType());
		if (skillType == null) {
			return MmochatUtil.msgbox(req.getHallState(), "无此炼丹术!");
		}
		MmochatLifeSkill skill = me.getLifeSkill(skillType);
		if (skill == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有学过此炼丹术!");
		}

		MmochatPet pkPet = me.getPkPet();
		if (pkPet == null || pkPet.getPetType() != MmochatPetType.药兽) {
			switch (skillType) {
			case 三魂炼丹术: {
				// 北郊
				return MmochatUtil.msgbox(req.getHallState(), "请先去"
						+ MmochatUtil.wrapColor("北郊", Color.yellow)
						+ "捕捉一只药兽，并设置为参战!");
			}
			case 七魄炼丹术: {
				// 西郊
				return MmochatUtil.msgbox(req.getHallState(), "请先去"
						+ MmochatUtil.wrapColor("西郊", Color.yellow)
						+ "捕捉一只药兽，并设置为参战!");
			}
			case 妙手回春炼丹术:
			case 魂魄归位炼丹术: {
				// 东郊
				return MmochatUtil.msgbox(req.getHallState(), "请先去"
						+ MmochatUtil.wrapColor("东郊", Color.yellow)
						+ "捕捉一只药兽，并设置为参战!");
			}
			default:
				break;
			}
		}
		if (pkPet.getOwnerState() != MmochatObjOwnerState.未认主) {
			return MmochatUtil
					.msgbox(req.getHallState(), "此宠物尚在<认主>状态，无法用来修炼!");
		}
		switch (skillType) {
		case 三魂炼丹术: {
			// 北郊
			if (pkPet.getRoleType() != MmochatRoleType.野兽) {
				return MmochatUtil.msgbox(req.getHallState(), "请去"
						+ MmochatUtil.wrapColor("北郊", Color.magenta)
						+ "捕捉一只药兽，并设置为参战!当前参战药兽不是产自北郊!");
			}
			break;
		}
		case 七魄炼丹术: {
			// 西郊
			if (pkPet.getRoleType() != MmochatRoleType.食人草) {
				return MmochatUtil.msgbox(req.getHallState(), "请去"
						+ MmochatUtil.wrapColor("西郊", Color.magenta)
						+ "捕捉一只药兽，并设置为参战!当前参战药兽不是产自西郊!");
			}
			break;
		}
		case 妙手回春炼丹术:
		case 魂魄归位炼丹术: {
			// 东郊
			if (pkPet.getRoleType() != MmochatRoleType.树妖
					&& pkPet.getRoleType() != MmochatRoleType.树精) {
				return MmochatUtil.msgbox(req.getHallState(), "请去"
						+ MmochatUtil.wrapColor("东郊", Color.magenta)
						+ "捕捉一只药兽，并设置为参战!当前参战药兽不是产自东郊!");
			}
			break;
		}
		default:
			break;
		}
		// 5/6机率得40，1/6机率得100
		int addExp = 0;
		if (MmochatUtil.isInPercent(16)) {
			addExp = 100;
		} else {
			// 30-50
			addExp = MmochatUtil.getRandomValue(30, 50);
		}

		// 增加每日次数
		{
			MmochatDailyActivity activity = me.getDailyActivities().get(
					MmochatDailyActivityType.炼丹术);
			if (activity == null) {
				// 第一次参加此活动
				activity = new MmochatDailyActivity();
				activity.setType(MmochatDailyActivityType.炼丹术);
				me.addDailyActivity(activity);
			}
			if (activity != null) {
				Date now = new Date();
				Date lastDate = new Date(activity.getLastTime());
				if (now.getDate() == lastDate.getDate()
						&& now.getMonth() == lastDate.getMonth()
						&& now.getYear() == lastDate.getYear()) {
					// 今天参加过
					if (activity.getDailyTimes() >= activity.getType()
							.getMaxDailyTimes()) {
						return MmochatUtil.msgbox(req.getHallState(), "每天只能进行"
								+ activity.getType().getMaxDailyTimes()
								+ "次修炼，请明天再来!");
					} else {
						activity.addDailyTimes(1);
					}
				} else {
					// 今天没有参加过此活动
					activity.setLastTime(now.getTime());
					activity.setDailyTimes(1);
				}
			}
		}

		// 增加技能经验
		skill.addExp(addExp);
		// 删除宠物
		me.delPet(pkPet.getId());
		try {
			MmochatDao.updateLifeSkillAndPets(me);
		} catch (Exception e) {
			e.printStackTrace();
			me.addPet(pkPet);
			skill.addExp(-1 * addExp);
			return MmochatUtil.msgbox(req.getHallState(), "数据库忙,请稍候再试!");
		}
		Mmochat_5_14S pack = new Mmochat_5_14S();
		pack.setDelPetId(pkPet.getId());
		MmochatUtil.sendCommonPack(me, pack);

		return MmochatUtil.msgbox(req.getHallState(), "修炼成功!您的"
				+ skillType.toString() + "增加了"
				+ MmochatUtil.wrapColor(addExp + "点经验", Color.yellow) + "!");
	}

	// 开始炼丹
	@SkymobiService
	public SkymobiProtocolMessage handleC_9_59(SkymobiHandler handler,
			Mmochat_9_59C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		MmochatLifeSkillType skillType = MmochatLifeSkillType.get(req
				.getSkillType());
		if (skillType == null) {
			return MmochatUtil.msgbox(req.getHallState(), "无此炼丹术!");
		}
		MmochatLifeSkill skill = me.getLifeSkill(skillType);
		if (skill == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有学过此炼丹术!");
		}

		MmochatDailyActivityType actType = null;
		List<MmochatMaterialType> materialsNeed = new ArrayList<MmochatMaterialType>();
		List<MmochatDrugType> drugsNeed = new ArrayList<MmochatDrugType>();

		switch (skillType) {
		case 三魂炼丹术: {
			actType = MmochatDailyActivityType.炼制三魂丹;
			materialsNeed.add(MmochatMaterialType.灵兽内丹);
			drugsNeed.add(MmochatDrugType.补气丹);
			break;
		}
		case 七魄炼丹术: {
			actType = MmochatDailyActivityType.炼制七魄丹;
			materialsNeed.add(MmochatMaterialType.巨兽皮);
			drugsNeed.add(MmochatDrugType.补血丸);
			break;
		}
		case 妙手回春炼丹术: {
			actType = MmochatDailyActivityType.炼制回春丹;
			materialsNeed.add(MmochatMaterialType.女妖之心);
			drugsNeed.add(MmochatDrugType.人参果);
			break;
		}
		case 魂魄归位炼丹术: {
			actType = MmochatDailyActivityType.炼制归位丹;
			materialsNeed.add(MmochatMaterialType.疯神玉简);
			drugsNeed.add(MmochatDrugType.菩提果);
			break;
		}
		default:
			break;
		}

		synchronized (me) {
			MmochatDailyActivity activity = me.getDailyActivities()
					.get(actType);
			if (activity == null) {
				// 第一次参加此活动
				activity = new MmochatDailyActivity();
				activity.setType(actType);
				me.addDailyActivity(activity);
			}
			if (activity.isInActivity()) {
				return MmochatUtil.msgbox(req.getHallState(), "您已经在炼丹了!");
			}
			// 检测是否有足够材料、药品、宠物
			for (MmochatMaterialType mType : materialsNeed) {
				if (me.getPackageObjNum(mType) < 20) {
					return MmochatUtil.msgbox(req.getHallState(), "您的包裹中"
							+ mType.toString() + "不足20个，无法炼制!");
				}
			}
			for (MmochatDrugType mType : drugsNeed) {
				if (me.getPackageObjNum(mType) < 20) {
					return MmochatUtil.msgbox(req.getHallState(), "您的包裹中"
							+ mType.toString() + "不足20个，无法炼制!");
				}
			}
			MmochatPet pkPet = me.getPkPet();
			switch (skillType) {
			case 三魂炼丹术: {
				if (pkPet == null || pkPet.getPetType() != MmochatPetType.药兽
						|| pkPet.getRoleType() != MmochatRoleType.野兽) {
					return MmochatUtil.msgbox(req.getHallState(), "请去"
							+ MmochatUtil.wrapColor("北郊", Color.green) + "捕捉1只"
							+ MmochatUtil.wrapColor("野兽(药兽)", Color.yellow)
							+ "，并设置参战。");
				}
				break;
			}
			case 七魄炼丹术: {
				if (pkPet == null || pkPet.getPetType() != MmochatPetType.药兽
						|| pkPet.getRoleType() != MmochatRoleType.食人草) {
					return MmochatUtil.msgbox(req.getHallState(), "请去"
							+ MmochatUtil.wrapColor("西郊", Color.green) + "捕捉1只"
							+ MmochatUtil.wrapColor("食人草(药兽)", Color.yellow)
							+ "，并设置参战。");
				}
				break;
			}
			case 妙手回春炼丹术:
			case 魂魄归位炼丹术: {
				if (pkPet == null
						|| pkPet.getPetType() != MmochatPetType.药兽
						|| (pkPet.getRoleType() != MmochatRoleType.树妖 && pkPet
								.getRoleType() != MmochatRoleType.树精)) {
					return MmochatUtil.msgbox(req.getHallState(), "请去"
							+ MmochatUtil.wrapColor("东郊", Color.green)
							+ "捕捉1只"
							+ MmochatUtil.wrapColor("树妖(药兽)或树精(药兽)",
									Color.yellow) + "，并设置参战。");
				}
				break;
			}
			default:
				break;
			}
			if (pkPet.getOwnerState() != MmochatObjOwnerState.未认主) {
				return MmochatUtil.msgbox(req.getHallState(),
						"此宠物尚在<认主>状态，无法用来炼丹!");
			}
			// 扣除材料药品宠物
			for (MmochatMaterialType mType : materialsNeed) {
				me.delPackageObj(mType, 20);
			}
			for (MmochatDrugType mType : drugsNeed) {
				me.delPackageObj(mType, 20);
			}
			// 删除宠物
			me.delPet(pkPet.getId());

			Mmochat_5_14S pack = new Mmochat_5_14S();
			pack.setDelPetId(pkPet.getId());
			MmochatUtil.sendCommonPack(me, pack);

			activity.setLastTime(System.currentTimeMillis());
			activity.setInActivity(true);

			try {
				MmochatDao.updateRoleDailyActivityAndPackageAndPet(me);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		// 我要炼丹
		CreateTlvWin tlv = getMakeDrugTlv(me, skillType);
		if (tlv != null) {
			CreateMsgBoxTLV msgbox = new CreateMsgBoxTLV();
			msgbox.setContent(new Text("开始炼制!"));
			msgbox.setLeftName("确定");
			return MmochatUtil.tlvResponse(req.getHallState(), new CloseWinTLV(
					MmochatConstant.TLV_WIN_LIFE_SKILL2), tlv, msgbox);
		}
		return null;
	}

	// 领取丹药
	@SkymobiService
	public SkymobiProtocolMessage handleC_9_60(SkymobiHandler handler,
			Mmochat_9_60C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		MmochatLifeSkillType skillType = MmochatLifeSkillType.get(req
				.getSkillType());
		if (skillType == null) {
			return MmochatUtil.msgbox(req.getHallState(), "无此炼丹术!");
		}
		MmochatLifeSkill skill = me.getLifeSkill(skillType);
		if (skill == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有学过此炼丹术!");
		}

		MmochatDailyActivityType actType = null;
		MmochatPropType propType = null;
		switch (skillType) {
		case 三魂炼丹术: {
			actType = MmochatDailyActivityType.炼制三魂丹;
			propType = MmochatPropType.三魂丹;
			break;
		}
		case 七魄炼丹术: {
			actType = MmochatDailyActivityType.炼制七魄丹;
			propType = MmochatPropType.七魄丹;
			break;
		}
		case 妙手回春炼丹术: {
			actType = MmochatDailyActivityType.炼制回春丹;
			propType = MmochatPropType.妙手回春丹;
			break;
		}
		case 魂魄归位炼丹术: {
			actType = MmochatDailyActivityType.炼制归位丹;
			propType = MmochatPropType.魂魄归位丹;
			break;
		}
		default:
			break;
		}

		synchronized (me) {
			MmochatDailyActivity activity = me.getDailyActivities()
					.get(actType);
			if (activity == null) {
				// 第一次参加此活动
				activity = new MmochatDailyActivity();
				activity.setType(actType);
				me.addDailyActivity(activity);
			}
			if (!activity.isInActivity()) {
				return MmochatUtil.msgbox(req.getHallState(), "您还没有开始炼丹!");
			}
			long costTime = System.currentTimeMillis() - activity.getLastTime();
			if (costTime < MmochatConstant.lifeDrugCreateTime) {
				return MmochatUtil.msgbox(req.getHallState(), "丹药尚未炼成!");
			}

			// 丹药炼成
			if (!me.hasEnoughPackageSpace(1)) {
				return MmochatUtil.msgbox(req.getHallState(),
						"您的包裹已满，请先整理出一个空位!");
			}

			// 增加丹药
			MmochatProp prop = new MmochatProp(propType);
			prop.setPropLevel(skill.getLevel());
			me.addObjectToPackage(prop);

			activity.setLastTime(System.currentTimeMillis());
			activity.setInActivity(false);

			try {
				MmochatDao.updateRoleDailyActivityAndPackage(me);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		// 我要炼丹
		CreateTlvWin tlv = getMakeDrugTlv(me, skillType);
		if (tlv != null) {
			CreateMsgBoxTLV msgbox = new CreateMsgBoxTLV();
			msgbox.setContent(new Text("恭喜!丹药炼制成功!请到包裹中查收!"));
			msgbox.setLeftName("确定");
			return MmochatUtil.tlvResponse(req.getHallState(), new CloseWinTLV(
					MmochatConstant.TLV_WIN_LIFE_SKILL2), tlv, msgbox);
		}
		return null;
	}
}
